import { StationNetwork } from "GameLoop/Path/StationNetwork";
import { WhiteListManager } from "GameLoop/WorldState/EngineConfig/WhiteListManager";
import { PositionHelper } from "utils/position/PositionHelper";


 
/**
 * 房间位置拓展
 */
 export default class RoomExtension extends Room 
 {

    private _energyAvailable?:number;
    private _blockPosition!:Set<number>;

    public  isSafe()
    {
        const cacheevents = global.world.PollEvents(this.name);
        if(cacheevents && cacheevents.isBeAttacked())
        // const cachehostilecreep = global.world.PollHostiles(this.name);
        // if(cachehostilecreep && cachehostilecreep.damage_sum>0)
        {
            Memory.Debug[`获取房间安全状态:${this.name}`] = "不安全";
            return false
        }
        Memory.Debug[`获取房间安全状态:${this.name}`] = "安全";
        return true;
    }

    public  registerBlocks(pos:RoomPosition[])
    {
        if(!this._blockPosition)
        {
            this._blockPosition = new Set<number>();
        }
        for(const e of pos)
        {
            this._blockPosition.add(e.x+e.y*50);
        }
    }
    public getBlock()
    {
        if(this._blockPosition)
        {
            return Array.from(this._blockPosition);
        }
        return []
    }
    public isBlock(pos:RoomPosition)
    {
        if(!this._blockPosition)
        {
            return false;
        }
        return this._blockPosition.has(pos.x+pos.y*50)
    }

    get energyAvailable_Cache()
    {
        if(!this._energyAvailable)
        {
            this._energyAvailable = this.energyAvailable;
        }
        return this._energyAvailable
    }
    set energyAvailable_Cache(val:number)
    {
        this._energyAvailable = val;
    }
 
    IsMy():boolean
    {
        if(this.controller && this.controller.my)
        {
            return true;
        };
        return false;
    }
    public isMyResverse()
    {
        if(this.controller &&global.UserName && this.controller.reservation?.username == global.UserName)
        {
            return true;
        }
        return false;
    }
    IsFriend():boolean
    {
        if(!this.controller)
        {
            return false;
        }
        if( this.controller.my)
        {
            return true;
        };
        if(this.controller.owner)
        {
            if(WhiteListManager.IsFriend(this.controller.owner?.username))
            {
                return true;
            }
        }
        return false;
    }
    
    // 房间类型   1 主房 2.跳板房 3.外矿房 4.冲级房 5.过道房 6.中央9房
    public  getType():number
    {
        // 1 主房 2.跳板房 3.外矿房 4.冲级房 5.过道房 6.中央9房
        if( this.memory?.config?.type)
        {
            return this.memory?.config?.type
        }
        return 0;
    }

    
    public isEnergyPoor():boolean
    {
       const spawn = this.getEntity("SpawnEntity")
       if(spawn.getContainer().length <2 && this.controller!.level>2)
       {
           return true
       }
       return !(this.memory.roleStatus["mine"]?.length>0 &&  this.memory.roleStatus["porter"]?.length>0)
    }

    /**
     * 查找集合点,默认都是4人队的集合点
     * @param nabRoom 相邻房间名
     * @param key   这个集合点被占用的唯一ID
     * @return 返回集合点队长位置
     */
    public findAssemble(key:string,from:string,to:string)
    {
         
        const path = StationNetwork.getInstance.findRoute(from,to,1);
        if(!path)
        {
            return undefined;
        }
        const rooms= path.value.route;

        const exits = Game.map.describeExits(rooms[0]);

        for(const key in exits)
        {
            if(rooms.length==1 || exits[key as ExitKey] == rooms[1])
            {
                // 出口中心点
                const pos = PositionHelper.getExitCenter(key as ExitKey, rooms[0]);
                if(pos.x <4)
                {
                    pos.x=3;
                }else if (pos.x>46)
                {
                    pos.x=46
                }
                if(pos.y <4)
                {
                    pos.y=3;
                }else if (pos.y>46)
                {
                    pos.y=46;
                }
                return pos;
                // 查找最近的rampart
                // const rampart = this.getEntity(entity_type.rampart);
                // const defenderpos = rampart.findDefendPosition(pos);

                // return defenderpos;
            }
        }
        return undefined;
    }

}
